package com.kl.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.kl.domain.TbContactsDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.TbContacts;
import com.kl.entity.TbTaxpayer;
import com.kl.entity.UserScore;
import com.kl.service.ITbContactsService;
import com.kl.service.IUserScoreService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PrimitiveIterator;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("tbContactsController")
public class TbContactsController {

    @Autowired
    private ITbContactsService contactsService;

    //联系人信息查询接口
    @ApiOperation(value = "联系人信息查询接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userName",value = "用户名",required = true,paramType = "query")
    })
    @GetMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result<List<TbContactsDTO>> selectById(@RequestParam String userName){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        List<TbContactsDTO> tbContactsDTOS = null;
        try {
            //pageList = userMapper.selectPage(page, null);
            List<TbContactsDTO> contactsList = contactsService.selectByUserName(userName);
            String string = JSONObject.toJSONString(contactsList);
            tbContactsDTOS = JSONObject.parseArray(string, TbContactsDTO.class);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(tbContactsDTOS);
    }

    //联系人信息增加接口
    @ApiOperation(value = "联系人信息增加接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "tbContactsDTO",value = "联系人实体类",required = true,paramType = "body")
    })
    @PostMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result inserInto(@RequestBody TbContactsDTO tbContactsDTO){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        TbContacts tbContacts = new TbContacts();
        BeanUtils.copyProperties(tbContactsDTO,tbContacts);
        System.out.println(tbContacts);
        try {
            contactsService.insertInto(tbContacts);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //联系人信息修改接口
    @ApiOperation(value = "联系人信息修改接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "tbContactsDTO",value = "联系人实体类",required = true,paramType = "body")
    })
    @PutMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result updateInfo(@RequestBody TbContactsDTO tbContactsDTO){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        TbContacts tbContacts = new TbContacts();
        BeanUtils.copyProperties(tbContactsDTO,tbContacts);
        try {
            contactsService.updateById(tbContacts);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //联系人信息id删除接口
    @ApiOperation(value = "联系人信息删除接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "tbId",value = "联系人ID",required = true,paramType = "query")
    })
    @DeleteMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result deleteById(@RequestParam Long tbId){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            contactsService.removeById(tbId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //联系人信息批量删除接口
    @ApiOperation(value = "联系人信息批量删除接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "ids",value = "删除id数",required = true,paramType = "body")
    })
    @DeleteMapping("deleteAll")
    public Result deleteAll(@RequestBody Integer[] ids) {
        try {
            contactsService.deleteBatchIds(Arrays.asList(ids));
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }
}
